package com.hsy.crm.web.service.asyn.marketprofit;

import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.domain.entity.SaleCostDetail;
import com.hsy.crm.web.domain.entity.StoreProductDetail;
import com.hsy.crm.web.mapper.SaleCostDetailMapper;
import com.hsy.crm.web.mapper.StoreProductDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 采购单反审核       出库
 *
 *
 * 如果采购明细有销售  则该库存后续利润重算
 */
@Service
public class UnBuyProfitService {

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    BuyProfitService buyProfitService;

    @Autowired
    ProfitCommonService profitCommonService;

    @Autowired
    SaleCostDetailMapper saleCostDetailMapper;

    public void init(Message message, List<StoreProductDetail> storeProductDetails){
        for (StoreProductDetail storeProductDetail : storeProductDetails){
            this.init(message,storeProductDetail);
        }
    }

    private void init(Message message,StoreProductDetail storeProductDetail){

        storeProductDetail.setCustomerId(message.getCustomerId());

        //采购单没有影响到销售则不管
        long storeProductDetailOutNumber = storeProductDetail.getOutNumber();
        if( storeProductDetailOutNumber == 0 ){
            return;
        }

        // 清理销售利润数据
        // 查询本次采购明细关联的销售明细 并清理关联数据  利润处理不能全部清空
        clearSaleStoreProductDetails(message,storeProductDetail);

        //删除本次采购明细对应的SaleCostDetail   上面已经删除
        // 删除本次采购明细 及 所有后续采购明细
        clearBuyStoreProductDetails(message,storeProductDetail);

        // 采购反审核 把上述代码中清空的销售重新处理
        // 处理一次未处理的销售明细
        buyProfitService.calcStoreProductDetailDone(message,storeProductDetail);

    }

    //清理销售利润数据
    private void clearSaleStoreProductDetails(Message message,StoreProductDetail storeProductDetail){
        long maxTime = -1;
        List<SaleCostDetail> saleCostDetails = profitCommonService.getInSaleCostDetail(message,storeProductDetail);
        for (SaleCostDetail saleCostDetail : saleCostDetails){
            profitCommonService.updateStoreProductDetail(message,saleCostDetail,saleCostDetail.getOutStoreProductDetailId());
            if( maxTime == -1 ){
                maxTime = saleCostDetail.getOutTime();
            }else if( maxTime < saleCostDetail.getOutTime() ){
                maxTime = saleCostDetail.getOutTime();
            }
        }

        //上面未处理的销售 需要处理
        StoreProductDetail maxTimeSelect = new StoreProductDetail();
        maxTimeSelect.setCustomerId(message.getCustomerId());
        maxTimeSelect.setStoreProductId(storeProductDetail.getStoreProductId());
        maxTimeSelect.setTime(maxTime);
        List<StoreProductDetail> storeProductDetails = storeProductDetailMapper.selectOutByTime(maxTimeSelect);
        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            profitCommonService.clearOutStoreProductDetail(message,outStoreProductDetail,Boolean.FALSE);
        }
    }

    //删除本次采购明细对应的SaleCostDetail   上面已经删除
    // 删除本次采购明细 及 所有后续采购明细
    private void clearBuyStoreProductDetails(Message message,StoreProductDetail storeProductDetail){
        storeProductDetailMapper.deleteByPrimaryKey(storeProductDetail);

        List<StoreProductDetail> storeProductDetails = storeProductDetailMapper.selectInByTime(storeProductDetail);
        for( StoreProductDetail buyStoreProductDetail : storeProductDetails ){
            buyStoreProductDetail.setCustomerId(message.getCustomerId());
            storeProductDetailMapper.clearOut(buyStoreProductDetail);

            List<SaleCostDetail> saleCostDetails = profitCommonService.getInSaleCostDetail(message,buyStoreProductDetail);
            for( SaleCostDetail saleCostDetail : saleCostDetails ){

                StoreProductDetail out = new StoreProductDetail();
                out.setCustomerId(message.getCustomerId());
                out.setId(saleCostDetail.getOutStoreProductDetailId());
                out = storeProductDetailMapper.selectByPrimaryKey(out);
                if( out.getOutNumber().longValue() > 0 ){
                    StoreProductDetail saleDetail = new StoreProductDetail();
                    saleDetail.setId(saleCostDetail.getOutStoreProductDetailId());
                    saleDetail.setCustomerId(message.getCustomerId());

                    saleDetail.setOutPrice(-saleCostDetail.getOutPrice());
                    saleDetail.setOutNumber(-saleCostDetail.getOutNumber());
                    saleDetail.setRelationEndSort(-1l);
                    saleDetail.setRelationStartSort(-1l);
                    storeProductDetailMapper.addOut(saleDetail);
                }
                saleCostDetail.setCustomerId(message.getCustomerId());
                saleCostDetailMapper.deleteByPrimaryKey(saleCostDetail);
            }
        }
    }

}
